package com.xinan.xs;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.render.JsonRender;
import com.xinan.hygl.Hygl;
import com.xinan.system.curd.AuthUser;
import com.xinan.utils.Constants;

public class XskdController  extends BaseController {

	public void index(){
		String card =getPara("card");
		setAttr("card", card);
		render("/WEB-INF/views/xskd/index.html");
	}
	@Before(SearchSql.class)
	public void gethy() {
//		String where = getAttr(Const.SEARCH_SQL);
		String where = "";
		String condition = getPara("search_EQ_card");
		where = "card='"+condition+"' or yhm='"+condition+"' or dh='"+condition
				+"' or cph='"+condition+"' or cph2='"+condition+"' or yhmjp='"+condition.toLowerCase()+"'";
		List<Record> newlist = Hygl.dao.findAlls(where);
		setAttr("newlist", newlist);
		render("/WEB-INF/views/xskd/form.html");
	}
	
	/**
	 * 资料修改
	 */
	public void zlxg() {
		String card =getPara("card");
		setAttr("card", card);
		String id = getPara("id");
		// List<Record> newlist = Db.find("select  * from     where    id=  '" +
		// id + "'");
		List<Record> newlist = Hygl.dao.findAlls("       id=  '" + id + "'");
		setAttr("newlist", newlist);
		render("/WEB-INF/views/xskd/hyupdate.html");
	}
	/**
	 * 积分兑换
	 */
	public void jfdh() {
		String card =getPara("card");
		setAttr("card", card);
		String id = getPara("id");
		String date="";
		Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        date = sdf.format(dt);
		// List<Record> newlist = Db.find("select  * from     where    id=  '" +
		// id + "'");
		List<Record> newlist = Hygl.dao.findAlls("       card=  '" + card + "'");
		setAttr("newlist", newlist);
		setAttr("date", date);
		render("/WEB-INF/views/xskd/jfdh.html");
	}
	/**
	 * 销售开单
	 */
	public void xskd() {
		String card =getPara("card");
		setAttr("card", card);
	    String lrydm = getAttr(Constants.USER_NAME);
	    String lrymc=getAttr(Constants.CN_NAME);
		String id = getPara("id");
		setAttr("lrydm", lrydm);
		setAttr("lrymc", lrymc);
        List<Record> user = Db.find("select username,cnname,id from auth_user");
		// List<Record> newlist = Db.find("select  * from     where    id=  '" +
		// id + "'");
		List<Record> newlist = Hygl.dao.findAlls("       id=  '" + id + "'");
		setAttr("newlist", newlist);
        setAttr("user", user);
        render("/WEB-INF/views/xskd/xfkd.html");
	}
	/**
	 * 挂失
	 */
	public void gs() {
		String card =getPara("card");
		setAttr("card", card);
		String id = getPara("id");
		// List<Record> newlist = Db.find("select  * from     where    id=  '" +
		// id + "'");
		List<Record> newlist = Hygl.dao.findAlls("       id=  '" + id + "'");

		setAttr("newlist", newlist);
		render("/WEB-INF/views/xskd/gs.html");
	}
	
	/**
	 * 销售充值
	 */
	public void cz() {
		String card =getPara("card");
		String id = getPara("id");

		List<Record> newlist = Hygl.dao.findAlls("       id=  '" + id + "'");
		setAttr("newlist", newlist);
		setAttr("card", card);

		render("/WEB-INF/views/xskd/cz.html");
	}
	public void zs(){
		String card =getPara("card");
		setAttr("card", card);

		render("/WEB-INF/views/xskd/zs.html");
	}
	/**
	 *修改
	 */
	public void update(){
		//返回卡信息
		String card =getPara("card");
		String id=getPara("id");
		List<Record> newlist = Hygl.dao.findAlls("       card=  '" + card + "'   and  id != '"+id+"'");

		if(newlist.size()>0){
		//	renderJson("{\"message\":\"卡号重复！请重新输入!\"}");
			render(new JsonRender("{\"message\":\"卡号重复！请重新输入!\",\"id\":\"0\"}").forIE());
		}  else {
		if (getModel(Hygl.class).update()) {
			render(new JsonRender("{\"message\":\"挂失成功!\",\"id\":\""+card+"\"}").forIE());
		} else {
			render(new JsonRender("{\"message\":\"挂失失败!\",\"id\":\"0\"}").forIE());
		}
		}
	}
	
	
	/**
	 *积分兑换
	 */
	public void jfdhupdate(){
		//返回卡信息
		String card =getPara("card");
		String id=getPara("id");
        int k=Db.update("insert into jfjl_hy ( wpid,wpmc,fwlxid,fwlxmc,xfjf,card) select s.wpid  ,s.wpmc , s.fwlxid,s.fwlxmc,s.xfjf ,'"+card+"'   from hygl_jfjl  s where s.id in (" + id + ")  ");

		if (k>0) {
			render(new JsonRender("{\"message\":\"赠送成功!\",\"id\":\""+card+"\"}").forIE());
		} else {
			render(new JsonRender("{\"message\":\"赠送失败!\",\"id\":\"0\"}").forIE());
		}
	}
	/**
	 *充值
	 */
    @Before(Tx.class)
	public void czje(){
		String id =getPara("id");
		String ye=getPara("ye"); //充值之前余额
		String yes=getPara("yes");
		String card =getPara("card");
		String czrq="";
		String czry = getAttr(Constants.USER_NAME);
		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		czrq = sdf.format(dt);
	//	Long czje=	Long.parseLong(yes)+Long.parseLong(ye);
		if (getModel(HyglCz.class).set("czje",yes)
				.set("cardid", id).set("card",card).
				set("czry", czry).set("czrq",czrq).remove("id").save()) {
			renderText(card);
		}else {
			renderText("0");
		}
	}
    /**
	 *开单
	 */
    @Before(Tx.class)
	public void spxskd(){
        String[] xfbz=getParaValues("xfbz");//标志
        String[] xfmc=getParaValues("xfmc");//消费名称
        String[] xfid=getParaValues("xfid");//消费id

        String[] xfsl=getParaValues("xfsl");//消费数量
        String[] xfje=getParaValues("xfje"); //消费金额
        String[] czymc=getParaValues("czymc");// 提成人员
        String[] xftc=getParaValues("xftc"); //消费提成
        String[] xfzk=getParaValues("xfzk"); //消费折扣
        String[] xfsjjg=getParaValues("xfsjjg"); //消费实际金额
        String card =getPara("card");
        String lrrq="";
        String czrydm = getAttr(Constants.USER_NAME);
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        lrrq = sdf.format(dt);
        boolean content=false;
        boolean count3 = false;

        for (int i=0;i<xfbz.length;i++){
            Record xx= new Record();
            xx.set("xfbz",xfbz[i]).set("xfmc",xfmc[i])
                    .set("xfsl",xfsl[i]).set("xfje",xfje[i])
                    .set("czymc",czymc[i]).set("xftc",xftc[i])
                    .set("xfzk",xfzk[i]).set("xfsjjg",xfsjjg[i]).set("xfid",xfid[i])
                    .set("card",card).set("lrrydm",czrydm).set("lrrq",lrrq);
            content=Db.save("hygl_xf",xx);
            System.out.println("============"+content);
            content=content&&content;
            System.out.println("============"+content);

        }
        if(content){
        	renderText(card);
        }else{
        	renderText("0");
        }

	}
    /**
	 *开单结账
	 */
    @Before(Tx.class)
	public void spxskdjz(){
        String[] xfbz=getParaValues("xfbz");//标志
        String[] xfmc=getParaValues("xfmc");//消费名称
        String[] xfid=getParaValues("xfid");//消费id

        String[] xfsl=getParaValues("xfsl");//消费数量
        String[] xfje=getParaValues("xfje"); //消费金额
        String[] czymc=getParaValues("czymc");// 提成人员
        String[] xftc=getParaValues("xftc"); //消费提成
        String[] xfzk=getParaValues("xfzk"); //消费折扣
        String[] xfsjjg=getParaValues("xfsjjg"); //消费实际金额
        String card =getPara("card");
        String xfsjjga =getPara("xfsjjga");
        String ye =getPara("ye");
        String vipcard =getPara("vipcard");
        String moneys =getPara("moneys");
        
        String lrrq="";
        String czrydm = getAttr(Constants.USER_NAME);
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        lrrq = sdf.format(dt);
        boolean content=false;
        boolean count3 = false;
        String ids="";
        for (int i=0;i<xfbz.length;i++){
            Record xx= new Record();
            xx.set("xfbz",xfbz[i]).set("xfmc",xfmc[i])
                    .set("xfsl",xfsl[i]).set("xfje",xfje[i])
                    .set("czymc",czymc[i]).set("xftc",xftc[i])
                    .set("xfzk",xfzk[i]).set("xfsjjg",xfsjjg[i]).set("xfid",xfid[i])
                    .set("card",card).set("lrrydm",czrydm).set("lrrq",lrrq);
            content=Db.save("hygl_xf",xx);
             ids = xx.get("id").toString() +","+ids;
            System.out.println("============"+ids);
            content=content&&content;
            System.out.println("============"+content);
        }
        ids=ids.substring(0,ids.length()-1);
        System.out.println("============"+ids);
        System.out.println("============"+ye +vipcard);
   	 	boolean  flg=savejz( card, ids,  vipcard,  moneys, xfsjjga,  ye);

        if(content&& flg){
        	renderText(card);
        }else{
        	renderText("0");
        }

	}
    
    
    /**
     * 结账
     *
     */
    @Before(Tx.class)
    public Boolean savejz(String card,String ida, String vipcard, String moneys,String xfsjjga, String ye) {

    //	String ye=getPara("ye");
        String jzrq="";
        String czrydm = getAttr(Constants.USER_NAME);
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        jzrq = sdf.format(dt);
        Record xx=new Record();
        xx.set("card",card).set("ida", ida).set("vipcard", vipcard).set("moneys", moneys).set("xfsjjga", xfsjjga)
                .set("jzrq", jzrq).set("czrydm",czrydm) ;
        Boolean cont=Db.save("hygl_jz",xx);
      
      
        int k=Db.update("update hygl_xf set yxbz=1  where id in (" + ida + ")");
        Db.update("insert into cpgl_ck ( wpid,cksl,ckje,ckyy) select s.xfid  ,s.xfsl , s.xfje,'1'  from hygl_xf  s where s.id in (" + ida + ")   and  xfbz='sptj' ");
        if(k>0 ){
            cont =true;
        } else {
            cont =false;
        }
        return cont; 
        
    }
}
